<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Chaoxing {

    // 数据库实例
    private $conn;

	public function __construct() {
        $this->conn = @mysqli_connect('120.131.6.196:3306', 'hzxsjs_zjk', 'CDoaq1AS4k05UaKU', 'jw_data');
       
        if ($this->conn == false) {
            throw new Exception("'Mysql could not connect:：" . mysqli_connect_error());
        }
        $this->conn->query('SET NAMES utf8');
    }

    // 通过id获取学校信息
    public function get_school_info($id){
        $sql = "SELECT * FROM `t_school` WHERE schoolId = '{$id}'";
        $res = $this->query_row($sql);
        return $res;
    }

    // 获取学校接口
    public function get_school_list(){
       
        $sql = "SELECT * FROM `t_school`";
        $res = $this->query($sql);
        return $res;
        // echo '<pre>';var_export($res);
    }
    // 获取年级接口
    public function get_grade_list($schoolId){
       
        $sql = "SELECT * FROM `t_grade`";
        // $sql = "SELECT * FROM `t_grade` WHERE `schoolId` = '{$schoolId}'";
        $res = $this->query($sql);
        return $res;
    }
    // 获取课室接口
    public function get_location_list($schoolId){

        $sql = "SELECT * FROM `t_location`";
        // $sql = "SELECT * FROM `t_location` WHERE `schoolId` = '{$schoolId}'";
        $res = $this->query($sql);
        return $res;
    }
    // 获取班级接口
    public function get_class_list($schoolId){
    
        $sql = "SELECT * FROM `t_class`";
        // $sql = "SELECT * FROM `t_class` WHERE `schoolId` = '{$schoolId}'";
        $res = $this->query($sql);
        return $res;
    }
    // 获取教师接口
    public function get_teacher_list($schoolId){

        $sql = "SELECT * FROM `t_teacher`";
        // $sql = "SELECT * FROM `t_class` WHERE `schoolId` = '{$schoolId}'";
        $res = $this->query($sql);
        return $res;
    }
    // 获取学生接口
    public function get_student_list($schoolId){
    
        $sql = "SELECT * FROM `t_student`";
        // $sql = "SELECT * FROM `t_student` WHERE `schoolId` = '{$schoolId}'";
        $res = $this->query($sql);
        return $res;
    }
    // 获取科目接口
    public function get_subject_list($schoolId){

        $sql = "SELECT * FROM `t_lesson`";
        // $sql = "SELECT * FROM `t_lesson` WHERE `schoolId` = '{$schoolId}'";
        $res = $this->query($sql);
        return $res;
    }
    // 获取班级接口
    public function get_schedule_list($schoolId){

        $sql = "SELECT * FROM `t_schedule`";
        // $sql = "SELECT * FROM `t_schedule` WHERE `schoolId` = '{$schoolId}'";
        $res = $this->query($sql);
        return $res;
    }

    // 查询数据库
    private function query($sql) {
        $res = array();

        if($result = mysqli_query($this->conn, $sql)){
            if(mysqli_num_rows($result) > 0){
                while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                    // var_export($row);
                    // print_r($row['name']);
                    array_push($res, $row);
                }
                // 释放结果集
                mysqli_free_result($result);
            }
        } else{
            throw new Exception("sql语句错误 '$sql' ：" . mysqli_error($this->conn));
        }

        return $res;
    }

    // 查询数据库，单条数据
    private function query_row($sql) {
        $res = array();

        if($result = mysqli_query($this->conn, $sql)){
            if(mysqli_num_rows($result) > 0){
                $res = mysqli_fetch_array($result, MYSQLI_ASSOC);
                // 释放结果集
                mysqli_free_result($result);
            }
        } else{
            throw new Exception("sql语句错误 '$sql' ：" . mysqli_error($this->conn));
        }

        return $res;
    }
    
}